package com.atguigu.tingshu.order.startegy.impl;
import java.math.BigDecimal;

import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.order.service.OrderDerateService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.startegy.SaveOrderStrategy;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AccountSaveOrderStrategy implements SaveOrderStrategy {

    @Autowired
    private UserAccountFeignClient userAccountFeignClient;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderDerateService orderDerateService;

    @Autowired
    private KafkaService kafkaService;

    @Override
    public OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) {
        //获取当前用户登录的id
        Long userId = AuthContextHolder.getUserId();
        //封装数据
        AccountLockVo accountLockVo = new AccountLockVo();
        accountLockVo.setOrderNo(orderInfoVo.getTradeNo());
        accountLockVo.setUserId(userId);
        accountLockVo.setAmount(orderInfoVo.getOrderAmount());
        accountLockVo.setContent(orderInfoVo.getOrderDetailVoList().get(0).getItemName());
        Result<AccountLockResultVo> accountLockResultVoResult = userAccountFeignClient.checkAndLock(accountLockVo);
        Integer code = accountLockResultVoResult.getCode();
        if( code != 200){ // 判断响应码
            throw new GuiguException(code,accountLockResultVoResult.getMessage());
        }
        //保存订单相关数据
        try {
            saveOrderAndDetailAndOrderDerate(orderInfoVo,orderInfoService,orderDetailService,orderDerateService);
        }catch (Exception e){
            e.printStackTrace();
            //异常 手动解锁
            kafkaService.sendMsg(KafkaConstant.QUEUE_ACCOUNT_UNLOCK,orderInfoVo.getTradeNo());
            //抛出异常
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        return orderInfoVo;
    }

    @Override
    public String getPayWay() {
        return SystemConstant.ORDER_PAY_ACCOUNT;
    }

    //保存订单数据后的抽象方法
    @Override
    public void saveOrderProcess(String orderNo) {
        orderInfoService.orderPaySuccess(orderNo);
        kafkaService.sendMsg(KafkaConstant.QUEUE_ACCOUNT_MINUS,orderNo);
    }
}
